package com.windoent.yugioh.widget.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.windoent.yugioh.R;
import com.windoent.yugioh.adapter.brvah.MeAuthAdapter;
import com.windoent.yugioh.adapter.brvah.PointFilterAdapter;
import com.windoent.yugioh.bean.RankListBean;
import com.windoent.yugioh.help.recycle.RecycleViewNolastLineDivider;
import com.windoent.yugioh.utils.TDevice;

import java.util.List;
import java.util.Objects;

/**
 * author : robert
 * date   : 2021/5/8 16:31
 * desc   :店家认证
 */
public class PointPopUpWindow extends PopupWindow {

    private Context context;
    private List<RankListBean.DataBean.AllSeasonBean> data;
    private OnInterface listener;
    private RecyclerView recyclerView;
    private float marginRight;

    public PointPopUpWindow(Context context, List<RankListBean.DataBean.AllSeasonBean> data, float margin, OnInterface onInterface) {
        this.context = context;
        this.data = data;
        this.listener = onInterface;
        this.marginRight = margin;
        initalize();
        setMarginRv();
    }

    private void setMarginRv() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) recyclerView.getLayoutParams();
        layoutParams.rightMargin = (int) marginRight;
        recyclerView.setLayoutParams(layoutParams);
    }

    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.pop_meinfo_auth_select_dialog, null);
        recyclerView = view.findViewById(R.id.recycler_view);
        setContentView(view);
        //设置宽高
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        initWindow();
        initData();
    }

    private void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        //最后一个不显示分割线且自定义分割线
        RecycleViewNolastLineDivider gridItemDecoration = new RecycleViewNolastLineDivider(context, DividerItemDecoration.VERTICAL);
        gridItemDecoration.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(context, R.drawable.drawable_recyclerview_divider)));
        recyclerView.addItemDecoration(gridItemDecoration);
        PointFilterAdapter adapter = new PointFilterAdapter();
        adapter.setNewInstance(data);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> baseQuickAdapter, @NonNull View view, int position) {
                if (listener != null) {
                    List<RankListBean.DataBean.AllSeasonBean> data = adapter.getData();
                    if (data != null) {
                        RankListBean.DataBean.AllSeasonBean s = data.get(position);
                        listener.onSelect(s);
                        dismiss();
                    }
                }
            }
        });

    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
    }     //设置添加屏幕的背景透明度

    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view, int w, int h) {        //弹窗位置设置
        showAsDropDown(view, (int) -(TDevice.dpToPixel(w)), (int) -(TDevice.dpToPixel(h)));
    }


    public interface OnInterface {
        void onSelect(RankListBean.DataBean.AllSeasonBean s);
    }
}
